﻿param($exportPath)

$parentPath=(Split-Path $PSCommandPath -Parent)

Import-Module -Force -DisableNameChecking $parentPath\..\BuildDeployment\libAssertion.psm1



$computers=(Get-ADComputer -Filter {(name -notlike "*v-*")}).Name | Sort-Object

Assert-Directory -Path $exportPath

$scriptBlock= 
    {
        param($computerName,$exportPath)

        Import-Module $using:parentPath\PSRemoteRegistry\PSRemoteRegistry.psm1
        

        if($computerName -inotlike "*v-*")
        {
            $connectionTest=Test-Connection -Count 1 -ComputerName $computerName -ErrorAction SilentlyContinue
            if($connectionTest)
            {
                try
                {
                    Write-Output "Checking Remote Registry on $computerName..."
                    $remoteRegistryService=Get-Service -ComputerName $computerName -Name RemoteRegistry -ErrorAction SilentlyContinue
                    if($remoteRegistryService.Status -eq "Stopped")
                    {
                        Write-Output "Starting service on $computerName..."
                        Get-Service -ComputerName $computerName -Name RemoteRegistry -ErrorAction SilentlyContinue | Start-Service -ErrorAction SilentlyContinue
                        Write-Output "Done!"
                    }   
                    $remoteRegistryService=Get-Service -ComputerName $computerName -Name RemoteRegistry -ErrorAction SilentlyContinue
                }
                catch
                {
                    Write-Output $_.Message
                }

                if($remoteRegistryService.Status -eq "Running")
                {
                    Get-RegValue -Hive LocalMachine -Key "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" -Value Release -ComputerName $computerName |
                    Select ComputerName, @{
                      name="Product"
                      expression={
                          switch($_.Data) {
                            378389 { [Version]"4.5" }
                            378675 { [Version]"4.5.1" }
                            378758 { [Version]"4.5.1" }
                            379893 { [Version]"4.5.2" }
                            393295 { [Version]"4.6" }
                            393297 { [Version]"4.6" }
                            394254 { [Version]"4.6.1" }
                            394271 { [Version]"4.6.1" }
                          }
                        }
                    } | Export-Csv -Path $exportPath\$computerName.csv -Delimiter "^" -Force -NoTypeInformation
                    
                    
                }
                else
                {
                    Write-Output "Remote registry is not running on $computerName"
                }
            }
            else
            {
                Write-Output "Cannot reach $computerName"
            }
        }
        else
        {
            Write-Output "$computerName is a service name,skipping."
        }
    }

foreach($computerName in $computers)
{
     
    $jobName="Gather .NET Framework versions on $computerName"

    $jobStarted=$false

    while(-not $jobStarted)
    {
        $activeJobCount=(Get-Job -Name "Gather .NET Framework versions*" | Where {$_.State -eq "Running"} | Measure-Object).Count
        if($activeJobCount -le 30)
        {
            Start-Job -Name $jobName -ScriptBlock $scriptBlock -ArgumentList $computerName,$exportPath
            $jobStarted=$true
        }
        else
        {
            Start-Sleep -Seconds 5
        }
    }
    
}


do
{
    Get-Job "Gather .NET Framework versions*"| Receive-Job
    Get-Job "Gather .NET Framework versions*" | where {$_.State -eq "Completed"} | Remove-Job
    Start-Sleep -Seconds 15   
}
while(((Get-Job -Name "Gather .NET Framework versions*" | Where {$_.State -eq "Running"} | Measure-Object).Count) -gt 0)
        

Get-ChildItem $exportPath\*.csv | ForEach-Object {Import-Csv -Delimiter "^"   $_} | Export-Csv -Delimiter "^" -NoTypeInformation .\FrameworkVersions.csv -Force
